/*
 * @Author: wangming
 * @Date: 2021-04-16 12:26:38
 * @LastEditors: wangming
 * @LastEditTime: 2022-05-27 17:20:24
 * @Description: 应用环境下自动登录 获取菜单生成路由
 */
import store from '@/store';
import { getAuthMenus } from './permission';
import { Accesstoken } from '@/utils/auth';
export function runApp(routers) {
  routers.beforeEach(async (to, from, next) => {
    let token = Accesstoken.getAccessToken();
    if (!!token) {
      if (process.env.VUE_APP_ROOT_URL === "./" && !store.getters.getAuthCenterInfo.isGetInfo) {
        store.commit('SET_AuthCenterInfo', { isGetInfo: true });
        store.dispatch('GetLicenseInfoDto');
      }
      store.dispatch('loadUserInfo');
      if (!!store.getters?.getMenusList?.length || store.getters?.getforWelcom) {
        next();
        return
      }
      let { appMenus } = await getAuthMenus();
      let accessRoutes = await store.dispatch('generateMenus', appMenus);
      routers.addRoutes(accessRoutes);
      if (to.name === 'app-login') {
        let { name, meta } = appMenus[0]?.children[0] || {};
        next({ name, query: { pageId: meta.pageId }, replace: true });
      } else {
        next({ ...to, replace: true });
      }
    } else if (to.name !== 'app-login' && to.name !== 'app-error') {
      next('/');
    } else {
      next();
    }
  });
}

